/*
 * \brief utils.h
 *
 * \verion 1.0.0
 * \author red li
 * \date 2009-08-04
 *
 */



#ifndef UTILS_H
#define UTILS_H

#include <stdio.h>
#include <string.h>

#include <assert.h>
#include "config.h"


typedef unsigned int utime_t;

#ifndef CURSES

#ifdef DEBUG
	#define DBG(fmt, args...) fprintf(stdout, "Debug: " fmt, ##args)
#else
	#define DBG(fmt, args...)
#endif

#define ERROR(fmt, args...) fprintf(stdout, "Error: " fmt, ## args)
#define LOG(fmt, args...) fprintf(stdout, "Log: " fmt, ## args)

#else

#ifdef DEBUG
	#define DBG(fmt, args...) mvprintw(10, 0, "DBG: " fmt, ## args) 
#else
	#define DBG(fmt, args...)
#endif

#define ERROR(fmt, args...) mvprintw(10, 0, "ERR: " fmt, ##args)
#define LOG(fmt, args...) mvprintw(10, 0, "LOG: " fmt, ##args)

#endif

#define mutex_init(st)		pthread_mutex_init(&(st)->mutex, NULL)
#define mutex_lock(st)		pthread_mutex_lock(&(st)->mutex)
#define mutex_unlock(st)	pthread_mutex_unlock(&(st)->mutex)
#define mutex_trylock(st)	pthread_mutex_trylock(&(st)->mutex)


/* --------------------------------------------------------------------------*/
/** 
 * @brief return string
 */
/* ----------------------------------------------------------------------------*/

static char error_string[][128] = 
{
	"",					//!< OTHER_RET			//!< this string can modified by 
	"Fatal error",		//!< FATAL_ERR
	"Success",			//!< SUCCESS
	"Normal error",		//!< ERR
	"Resend",			//!< resend
};



/* --------------------------------------------------------------------------*/
/** 
 * @brief return number
 */
/* ----------------------------------------------------------------------------*/

typedef enum ret_t
{
	OTHER_ERROR = -2,
	FATAL_ERR = -1,
	SUCCESS = 0,
	FAILED= 1,
	RESEND,
}ret_t;
/* --------------------------------------------------------------------------*/
/** 
 * @brief print ret string instead of ret_num
 * 
 * @param ret_num ret_t, return number, same as perror
 * 
 * @return none
 *
 * @review tested
 */
/* ----------------------------------------------------------------------------*/
#define perr(ret_num) fprintf(stderr, "Return string: %s\n", error_string[(ret_num) + 2])



/* --------------------------------------------------------------------------*/
/** 
 * @brief set the other return string
 * 
 * @param ret_str return string 
 * 
 * @return none
 *
 * @review tested
 */
/* ----------------------------------------------------------------------------*/
#define serr(ret_str){ assert(ret_str); \
					  strcpy(error_string[0], ret_str);}



/* --------------------------------------------------------------------------*/
/** 
 * @brief get current time by usec
 * 
 * @return currnet time(usec)
 * 
 * @review draft
 */
/* ----------------------------------------------------------------------------*/
inline utime_t get_time();
  
void utime_init();
#define TIME_GAP	10000


#endif
